package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.system.model.entity.Employee;
import com.bingxue.edu.management.system.model.query.EmployeeQuery;
import com.bingxue.edu.management.system.model.req.EmployeeCreateReq;
import com.bingxue.edu.management.system.model.req.EmployeePasswordUpdateReq;
import com.bingxue.edu.management.system.model.req.EmployeeStatusChangeReq;
import com.bingxue.edu.management.system.model.req.EmployeeUpdateReq;
import com.bingxue.edu.management.system.model.resp.EmployeeDetailResp;
import com.bingxue.edu.management.system.model.resp.EmployeeResp;
import com.bingxue.edu.management.system.service.EmployeeService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 员工接口
 *
 * @author panbo
 * @since 2024-07-03
 */
@Slf4j
@Tag(name = "员工接口")
@RestController
@RequestMapping("/system/employee")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;

    @Operation(summary = "查询列表")
//    @ApiResource("system:org:manage")
    @GetMapping("/list")
    public Response<List<EmployeeResp>> list(EmployeeQuery query) {
        List<EmployeeResp> list = employeeService.getEmployeeList(query);
        return Response.success(list);
    }

    @Operation(summary = "分页查询列表")
//    @ApiResource("system:org:manage")
    @GetMapping("/page")
    public Response<Pagination<EmployeeResp>> page(EmployeeQuery query, @Validated PageQuery pageQuery) {
        Page<EmployeeResp> page = employeeService.getEmployeePage(query, pageQuery);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询详情")
//    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<EmployeeDetailResp> detail(@PathVariable String id) {
        EmployeeDetailResp detail = employeeService.getMapper().selectOneWithRelationsByIdAs(id, EmployeeDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated EmployeeCreateReq req) {
        employeeService.createEmployee(req);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated EmployeeUpdateReq req, @PathVariable String id) {
        Employee employee = employeeService.copyById(req, id);
        employeeService.updateById(employee);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:org:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        employeeService.removeByIds(ids);
        return Response.success();
    }


    @Operation(summary="禁用/启用账户")
    @ApiResource("system:org:manage")
    @GetMapping("/change-status")
    public Response<Void> changeStatus(EmployeeStatusChangeReq query) {
            employeeService.changeStatus(query.getId(),query.getStatus());
            return Response.success();
    }

    @Operation(summary="重置密码")
    @ApiResource("system:org:manage")
    @PostMapping("/resetPassword")
    public Response<Void> resetPassword(@RequestBody @Validated EmployeePasswordUpdateReq req) {
            employeeService.resetPassword(req.getId(), req.getPassword());
            return Response.success();
    }
}
